package com.cisdi.blog.web.admin;

import com.cisdi.blog.dao.UserRepository;
import com.cisdi.blog.entity.User;
import com.cisdi.blog.util.BeanUtilsExt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author pengyangyan
 * @version 1.0.0
 * @date 2020年04月11日 20:04:00
 */
@Controller
@RequestMapping("/admin/users")
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping
    String showUser(Model model){
        User user = userRepository.findById((long) 1).get();
        model.addAttribute("user",user);
        return "admin/system";
    }

    @PostMapping("/update")
    String updateUser(User user){
        User one = userRepository.findById(user.getId()).get();
        BeanUtilsExt.copyBeanNotCopyNullValue(user,one);
        userRepository.save(one);
        return "redirect:/admin/users";
    }
}
